{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "mobile-structure",
   "metadata": {},
   "source": [
    "### 安装lightGBM\n",
    "> **sudo pip3 install lightgbm**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "velvet-treasure",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "4.4.0\n"
     ]
    }
   ],
   "source": [
    "import lightgbm\n",
    "\n",
    "print(lightgbm.__version__) # 版本号"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "instrumental-latin",
   "metadata": {},
   "source": [
    "## 一. 基于scikit-learn实现的LGBMRegressor和LGBMClassifier"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "confirmed-omaha",
   "metadata": {},
   "source": [
    "### （一）分类问题 LGBMClassifier"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "multiple-dispatch",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1 生成虚拟的数据集\n",
    "\n",
    "from sklearn.datasets import make_classification\n",
    "\n",
    "X, y = make_classification(n_samples=500, n_features=15, random_state=666) # 虚拟数据，样本5000个，特征15个"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "critical-embassy",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(500, 15)"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "quick-sampling",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(500,)"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "attached-utility",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([-0.53770788,  0.78150137,  0.33963353,  1.94970603,  4.6180342 ,\n",
       "       -0.24310921, -3.32868959, -2.34709313, -0.44572581,  2.0075885 ,\n",
       "        0.84122037,  2.24306922,  0.92976164, -1.31841369, -1.72482838])"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X[0,:] # 样本1的数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "renewable-methodology",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y[0] # 样本1的标签"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "metallic-guarantee",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "from sklearn.datasets import make_classification\n",
    "from sklearn.model_selection import cross_val_score # 交叉验证评估\n",
    "from sklearn.model_selection import RepeatedStratifiedKFold # 切分多折训练、评估\n",
    "from lightgbm import LGBMClassifier"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "characteristic-taylor",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "30轮的结果： [0.94 0.96 0.94 0.92 0.86 0.98 0.94 0.94 0.94 0.94 0.96 0.98 0.98 0.9\n",
      " 0.94 0.88 0.94 0.96 0.96 0.98 0.98 0.94 1.   0.94 0.98 0.96 0.94 0.94\n",
      " 0.9  0.96]\n",
      "Accuracy : 0.946\n"
     ]
    }
   ],
   "source": [
    "# 定义基模型\n",
    "model = LGBMClassifier()\n",
    "\n",
    "# 切分10折，重复3轮\n",
    "kf = RepeatedStratifiedKFold(n_splits=10, n_repeats=3, random_state=666)\n",
    "\n",
    "# 交叉训练、评估\n",
    "evaluate = cross_val_score(model, X, y, scoring='accuracy', cv=kf, n_jobs=-1)\n",
    "\n",
    "# 输出结果\n",
    "print(\"30轮的结果：\", evaluate)\n",
    "\n",
    "# 均值\n",
    "print(\"Accuracy : %.3f\" % np.mean(evaluate))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "precise-distribution",
   "metadata": {},
   "source": [
    "### （二）回归问题 LGBMRegressor"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "functional-syndrome",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.datasets import make_regression\n",
    "\n",
    "# 生成数据集\n",
    "X, y = make_regression(n_samples=500, n_features=15, noise=0.1, random_state=666)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "original-memorial",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(500, 15)"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "absolute-dating",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(500,)"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "prompt-superintendent",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.model_selection import cross_val_score\n",
    "from sklearn.model_selection import RepeatedKFold\n",
    "from lightgbm import LGBMRegressor"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "global-carter",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "mae -42.611\n"
     ]
    }
   ],
   "source": [
    "# 基模型对象\n",
    "model_reg = LGBMRegressor()\n",
    "\n",
    "# 数据集切分10折，重复3轮\n",
    "kf = RepeatedKFold(n_splits=10, n_repeats=3, random_state=666)\n",
    "\n",
    "# 模型训练，评估\n",
    "result = cross_val_score(model_reg, X, y, scoring='neg_mean_absolute_error', cv=kf, n_jobs=-1)\n",
    "\n",
    "# 输入结果\n",
    "print(\"mae %.3f\" % np.mean(result))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "personalized-swiss",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict_keys(['explained_variance', 'r2', 'max_error', 'neg_median_absolute_error', 'neg_mean_absolute_error', 'neg_mean_absolute_percentage_error', 'neg_mean_squared_error', 'neg_mean_squared_log_error', 'neg_root_mean_squared_error', 'neg_mean_poisson_deviance', 'neg_mean_gamma_deviance', 'accuracy', 'top_k_accuracy', 'roc_auc', 'roc_auc_ovr', 'roc_auc_ovo', 'roc_auc_ovr_weighted', 'roc_auc_ovo_weighted', 'balanced_accuracy', 'average_precision', 'neg_log_loss', 'neg_brier_score', 'adjusted_rand_score', 'rand_score', 'homogeneity_score', 'completeness_score', 'v_measure_score', 'mutual_info_score', 'adjusted_mutual_info_score', 'normalized_mutual_info_score', 'fowlkes_mallows_score', 'precision', 'precision_macro', 'precision_micro', 'precision_samples', 'precision_weighted', 'recall', 'recall_macro', 'recall_micro', 'recall_samples', 'recall_weighted', 'f1', 'f1_macro', 'f1_micro', 'f1_samples', 'f1_weighted', 'jaccard', 'jaccard_macro', 'jaccard_micro', 'jaccard_samples', 'jaccard_weighted'])"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from sklearn import metrics\n",
    "\n",
    "metrics.SCORERS.keys() # 查看scoring的关键字"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "essential-sampling",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
